package com.ctl.szmd.szmd.feature_wm.ui.fragment.pager1

import android.annotation.SuppressLint
import android.content.Context
import android.util.TypedValue
import android.view.View
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.ctl.indicator.lib.commonnavigator.CommonNavigator
import com.ctl.indicator.lib.commonnavigator.abs.CommonNavigatorAdapter
import com.ctl.indicator.lib.commonnavigator.abs.IPagerIndicator
import com.ctl.indicator.lib.commonnavigator.abs.IPagerTitleView
import com.ctl.indicator.lib.commonnavigator.indicators.LinePagerIndicator
import com.ctl.indicator.lib.commonnavigator.titles.ColorTransitionPagerTitleView
import com.ctl.indicator.lib.commonnavigator.titles.SimplePagerTitleView
import com.ctl.indicator.lib.help.ViewPagerHelper
import com.ctl.indicator.lib.util.UIUtil
import com.ctl.lib_common.base.ViewPagerFragmentStateAdapter
import com.ctl.lib_common.base.XFragmentPagerAdapter
import com.ctl.lib_common.extensions.getColor
import com.ctl.lib_common.extensions.singleClick
import com.ctl.szmd.szmd.feature_wm.R
import com.ctl.szmd.szmd.feature_wm.databinding.FragmentWalkIntoWmBinding
import com.ctl.szmd.szmd.feature_wm.ui.base.BaseWmBindingFragment
import kotlinx.android.synthetic.main.view_wm_tool_bar.*


class WalkIntoWmFragment : BaseWmBindingFragment<FragmentWalkIntoWmBinding>() {
    private val CHANNELS = arrayOf(
        "妇联简介",
        "下属组织",
        "服务热线"
    )
    private val mDataList = mutableListOf(*CHANNELS)
    override fun getLayoutId(): Int = R.layout.fragment_walk_into_wm

    override fun initView() {
        binding.apply {
            toolbar_title.text = "走进妇联"
            toolbar_left.visibility = View.VISIBLE
            toolbar_left.singleClick {
                findNavController().navigateUp()
            }
            val dataList: MutableList<Fragment> = mutableListOf()
            dataList.add(WmBriefFragment())
            dataList.add(SubOrgFragment())
            dataList.add(WmBriefFragment())
            viewPager2.offscreenPageLimit = dataList.size
            viewPager2.adapter = ViewPagerFragmentStateAdapter(
                this@WalkIntoWmFragment,
                dataList
            )
            viewPager2.isSaveEnabled = false

            val commonNavigator = CommonNavigator(requireContext())
            commonNavigator.setAdjustMode(true)
            commonNavigator.setAdapter(object : CommonNavigatorAdapter() {
                override fun getCount(): Int {
                    return dataList.size
                }

                @SuppressLint("InflateParams")
                override fun getTitleView(context: Context, index: Int): IPagerTitleView? {
                    val simplePagerTitleView: SimplePagerTitleView =
                        ColorTransitionPagerTitleView(context)
                    simplePagerTitleView.text = mDataList[index]
                    simplePagerTitleView.setTextSize(
                        TypedValue.COMPLEX_UNIT_SP, 16f
                    )
                    simplePagerTitleView.setNormalColor(R.color.color6.getColor(requireContext()))
                    simplePagerTitleView.setSelectedColor(R.color.colorWmRed.getColor(requireContext()))
                    simplePagerTitleView.setOnClickListener {
                        viewPager2.currentItem = index
                    }
                    return simplePagerTitleView
                }

                override fun getIndicator(context: Context): IPagerIndicator {
                    val indicator = LinePagerIndicator(context)
                    indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT)
                    indicator.setLineHeight(UIUtil.dip2px(context, 1.0).toFloat())
                    indicator.setColors(R.color.colorWmRed.getColor(requireContext()))
                    return indicator
                }
            })
            indicator.setNavigator(commonNavigator)
            ViewPagerHelper.bind(indicator, viewPager2)
        }
    }

    override fun onStart() {
        super.onStart()
        binding.viewPager2.adapter?.notifyItemChanged(binding.viewPager2.currentItem)
    }
}